package com.sdry.web.controller.gemini;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.base.BaseController;
import com.sdry.entity.*;
import com.sdry.service.gemini.UpmsSupplierService;
import com.sdry.utils.ResponseUtil;
import com.sdry.entity.UpmsSupplierExample.Criteria;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Gemini on 2018/7/31
 *
 * 供应商信息控制类
 */

@Controller
@RequestMapping("/base/supplier")
public class BaseSupplierInfoController extends BaseController {

    @Resource
    private UpmsSupplierService supplierService;

    /**
     * 去供应商信息页面
     * @return
     */
    @RequestMapping(value = "/baseInfoSupplier", method = RequestMethod.GET)
    public String baseInfoSupplier() {

        return "/gemini/info_supplier";
    }

    /**
     * 添加供应商信息
     * @param response 响应
     * @param supplier 供应商实体
     * @throws Exception 异常
     */
    @RequestMapping("/insertBaseSupplierInfo")
    public void insertSupplier(HttpServletResponse response, UpmsSupplier supplier) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        supplier.setSupplierDate(df.format(new Date()));//给录入时间赋值
        //添加
        int affact = supplierService.insert(supplier);
        ResponseUtil.write(response, affact);
    }

    /**
     * 带条件的分页查询供应商信息
     * @param request 请求
     * @param response 响应
     * @param limit 每页记录数
     * @param offset 页面
     * @param supplier 供应商实体
     * @throws Exception 异常
     */
    @RequestMapping("/selectBaseSupplierInfo")
    public void selectSupplierList(HttpServletRequest request, HttpServletResponse response,
                               @RequestParam(value = "limit",required = false)int limit,
                               @RequestParam(value = "offset",required = false)int offset,
                               UpmsSupplier supplier) throws Exception {

        PageHelper.startPage(offset, limit);

        UpmsSupplierExample example = new UpmsSupplierExample();
        Criteria criteria = example.createCriteria();
        if (supplier.getSupplierNumber() != null && !("").equals(supplier.getSupplierNumber())){
            criteria.andSupplierNumberLike("%"+supplier.getSupplierNumber()+"%");
        }

        if (supplier.getSupplierContact() != null && !("").equals(supplier.getSupplierContact())){
            criteria.andSupplierContactLike("%"+supplier.getSupplierContact()+"%");
        }
        List<UpmsSupplier> suppliers = supplierService.selectByExample(example);

        PageInfo pageInfo = new PageInfo(suppliers);

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total", pageInfo.getTotal());
        jsonObject.put("rows", pageInfo.getList());
        ResponseUtil.write(response, jsonObject);
    }

    /**
     * 根据id查询供应商信息
     * @param response 响应
     * @param id 供应商id
     * @throws Exception 异常
     */
    @RequestMapping("/selectBaseSupplierInfoById")
    public void selectSupplierById(HttpServletResponse response, Long id) throws Exception {

        UpmsSupplier supplier = supplierService.selectByPrimaryKey(id);
        //将对象转化为json
        JSONObject json = JSONObject.fromObject(supplier);
        ResponseUtil.write(response, json);
    }

    /**
     * 修改供应商信息
     * @param response 响应
     * @param supplier 供应商信息
     * @throws Exception 异常
     */
    @RequestMapping("/editBaseSupplierInfo")
    public void editSupplier( HttpServletResponse response,
                          UpmsSupplier supplier) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        supplier.setSupplierDate(df.format(new Date()));//给录入时间赋值
        //修改
        int affct = supplierService.updateByPrimaryKey(supplier);
        ResponseUtil.write(response, affct);
    }

    /**
     * 批量删除供应商信息
     * @param response 响应
     * @param ids 备品备件id
     * @throws Exception 异常
     */
    @RequestMapping("/deleteBaseSupplierInfo")
    public void deleteSupplier( HttpServletResponse response, String ids) throws Exception {
        int affact = supplierService.deleteByPrimaryKeys(ids);
        ResponseUtil.write(response, affact);
    }
}
